package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddTalentEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.TalentTypeEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.PersonnelPublishActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonnelPublishModel extends BaseViewModel {
    public ObservableField<AddTalentEntity> addTalentEntity = new ObservableField<>(new AddTalentEntity());
    private PersonnelPublishActivity activity;
    private SelfRepertory repertory;

    public PersonnelPublishModel(PersonnelPublishActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //有发布信息获取对应详情
    public void getPersonnerDetailModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getTalentDetail()
                .subscribe(new Consumer<BaseEntity<AddTalentEntity>>() {
                    @Override
                    public void accept(BaseEntity<AddTalentEntity> addTalentEntityBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (addTalentEntityBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showDetailView(addTalentEntityBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getPersonModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getPerson().enqueue(new Callback<BaseEntity<AddTalentEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AddTalentEntity>> call, Response<BaseEntity<AddTalentEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showDetailView(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AddTalentEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取所属行业和专业技能列表
    public void getTypeModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getTalentType()
                .subscribe(new Consumer<BaseEntity<List<TalentTypeEntity<List<TalentTypeEntity>>>>>() {
                    @Override
                    public void accept(BaseEntity<List<TalentTypeEntity<List<TalentTypeEntity>>>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showTypeView(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //获取默认所属行业下专业技能
    public void getDefaultSkillModel(String no, List<TalentTypeEntity<List<TalentTypeEntity>>> typeEntityList) {
        repertory.getDefaultTalentSkill(no, typeEntityList)
                .subscribe(new Consumer<List<TalentTypeEntity>>() {
                    @Override
                    public void accept(List<TalentTypeEntity> talentTypeEntities) throws Exception {
                        activity.showDefaultSkill(talentTypeEntities);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getLocateList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getCode().equals("200")) {
                    activity.showAddressView(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
}
